package com.example.student.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.student.bean.BaseResultBean;
import com.example.student.bean.Student;
import com.example.student.service.IStudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("student")
public class StudentController {
    @Autowired
    IStudentService studentService;

    @RequestMapping("all")
    public BaseResultBean getAll(){
        BaseResultBean resultBean = BaseResultBean.OK(studentService.list());
        return resultBean;
    }
    //添加
    @PostMapping("add")
    public BaseResultBean add(@RequestBody Student student){
        try {
            Date date=new Date();
            SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String dateStr=temp.format(date);
            student.updateTime = dateStr;
            boolean save = studentService.save(student);
            if (save) {
                return BaseResultBean.OK(null);
            }else{
                return BaseResultBean.SQL_fail();
            }
        }catch (Exception e){
            e.printStackTrace();
            return BaseResultBean.Server_fail();
        }
    }
    //删除
    @RequestMapping ("delete/{id}")
    public BaseResultBean delete(@PathVariable Integer id){
        try {
            boolean remove = studentService.removeById(id);
            if (remove) {
                return BaseResultBean.OK(null);
            }else{
                return BaseResultBean.SQL_fail();
            }
        }catch (Exception e){
            e.printStackTrace();
            return BaseResultBean.Server_fail();
        }
    }
    //更新
    @RequestMapping ("update")
    public BaseResultBean update(@RequestBody Student student){
        try {
            Date date=new Date();
            SimpleDateFormat temp=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
            String dateStr=temp.format(date);
            student.updateTime = dateStr;
            boolean update = studentService.update(student,null);
            if (update) {
                return BaseResultBean.OK(null);
            }else{
                return BaseResultBean.SQL_fail();
            }
        }catch (Exception e){
            e.printStackTrace();
            return BaseResultBean.Server_fail();
        }
    }
    //分页查询
    @PostMapping("getByPage/{current}/{size}")
    public BaseResultBean getByPage(@PathVariable Integer current,@PathVariable Integer size){
        if (size <0) return BaseResultBean.fail("size 必须大于0");
        if (current <1) return BaseResultBean.fail("current 必须大于1");
        try {
            IPage<Student> page = new Page<>(current,size);
            studentService.searchPage(page);
            BaseResultBean resultBean = BaseResultBean.OK(page,"查询成功");
            return resultBean;
        }catch (Exception e){
            e.printStackTrace();
            return BaseResultBean.Server_fail();
        }
    }
    //查详情
    @PostMapping("getById/{id}")
    public BaseResultBean getById(@PathVariable Integer id){
        try {
            Student student = studentService.searchById(id);
            if (student == null) {
                return BaseResultBean.fail("未查询到");
            }else{

                return BaseResultBean.OK(student);
            }
        }catch (Exception e){
            e.printStackTrace();
            return BaseResultBean.Server_fail();
        }
    }
}
